package com.xw.echo.core.nacos.client;

import com.alibaba.nacos.api.exception.NacosException;
import com.alibaba.nacos.api.naming.NamingService;
import com.xw.echo.core.nacos.ServiceEventListener;
import com.xw.echo.core.register.HostAndPort;
import lombok.extern.slf4j.Slf4j;

import java.util.List;


@Slf4j
public class NacosConsumer {
    private NamingService namingService;
    private String serviceName;
    public NacosConsumer(NamingService namingService,String serviceName) {
        this.serviceName=serviceName;
        this.namingService = namingService;
        subscribe(serviceName);
    }
    private void subscribe(String serviceName){
        try {
            namingService.subscribe(serviceName, new ServiceEventListener());
        } catch (NacosException e) {
            throw new RuntimeException(e);
        }
    }
    public List<HostAndPort> getAllInstanceIpAndPort()  {
        List<HostAndPort> serverAdders = AddrsLocalCache.get(serviceName);
        return serverAdders;
    }



}
